import os
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By


class  MouseOperation:
    #实例化对象
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://sahitest.com/demo/clicks.htm')

    #单击
    def click(self):
        time.sleep(3)
        bit=self.driver.find_element(By.XPATH,'//form/input[3]')
        bit.click()
    #右击
    def context_click(self):
        time.sleep(3)
        ac = ActionChains(self.driver)
        btn=self.driver.find_element(By.XPATH, '/html/body/form/input[4]')
        ac.context_click(btn).perform()

    #双击
    def double_click(self):
        time.sleep(3)
        ac = ActionChains(self.driver)
        body=self.driver.find_element(By.XPATH,'/html/body/form/input[2]')
        ac.double_click(body).perform()

    #拖动
    def drag_and_drop(self):
        self.driver.get("https://sahitest.com/demo/dragDropMooTools.htm")
        time.sleep(3)
        ac = ActionChains(self.driver)
        source = self.driver.find_element(By.ID, "dragger")#//*[@id="dragger"]
        target = self.driver.find_element(By.XPATH, "//div[4]")
        ac.drag_and_drop(source, target).perform()

    #悬停
    def move_to_element(self):
        self.driver.get("https://sahitest.com/demo/mouseover.htm")
        time.sleep(2)
        ac = ActionChains(self.driver)
        div = self.driver.find_element(By.XPATH, "//div/div")
        ac.move_to_element(div).perform()


    #停住
    def click_and_hold(self):
        file="file:///" +os.path.abspath("html") +"/mouse_hold.html"
        self.driver.get(file)
        ac = ActionChains(self.driver)
        abc= self.driver.find_element(By.ID, "btn1")
        ac.click_and_hold(abc).perform()
        time.sleep(10)


    def quit(self):
        time.sleep(3)
        self.driver.quit()



if __name__ == '__main__':
    mo=MouseOperation()
    # mo.click()
    # mo.context_click()
    # mo.double_click()
    # mo.drag_and_drop()
    # mo.move_to_element()
    mo.click_and_hold()
    mo.quit()

